package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.ConsumerSmsQO;
import com.xingkeduo.pojo.table.ConsumerSmsTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/2/19 10:32
 **/
@Component
public class ConsumerSmsJTO extends BaseJson {

    /**
     * 入参json
     *
     * @param json:
     * @return : com.xingkeduo.pojo.query.ConsumerQO
     * @author : gaozifeng
     * @date : 2019/2/28 11:00
     */
    public static ConsumerSmsQO createQueryForList(String json) throws Throwable {
        ConsumerSmsQO query = new ConsumerSmsQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setPhone(validString(root, "phone", 11, YesNoEnum.NO)).setSmsCode(validString(root, "smsCode", 6, YesNoEnum.NO)).setSmsType(validString(root, "smsType", 3, YesNoEnum.NO))
                .setConsumerId(validPrimary(root, "consumerId", YesNoEnum.NO));

        return query;
    }

    public static ConsumerSmsQO createQueryForPrimary(String json) throws Throwable {
        ConsumerSmsQO query = new ConsumerSmsQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");
        query.setId(validId(root, YesNoEnum.YES));
        return query;
    }

    /**
     * 返回实体信息
     *
     * @param table:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019/2/19 16:24
     */
    public static ObjectNode toJson(ConsumerSmsTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        if (table != null) {
            root.put("smsType", table.getSmsType());
            root.put("smsCode", table.getSmsCode());
            root.put("phone", table.getPhone());
            root.put("consumerId",table.getConsumerId());
            toBaseJson(root, table);
        }
        return root;
    }

    /**
     * 返回集合
     *
     * @param tables:
     * @return : com.fasterxml.jackson.databind.node.ArrayNode
     * @author : gaozifeng
     * @date : 2019/2/19 16:25
     */
    public static ArrayNode toJson(List<ConsumerSmsTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables)) {
            for (Iterator<ConsumerSmsTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }
        }
        return root;
    }

    public static ConsumerSmsTO createTableForNew(String json) throws Throwable {
        ConsumerSmsTO table = new ConsumerSmsTO();
        JsonNode root = validJson(json);
        if (root == null) {
            throw new InputParamException(InputParamException.Type.INVALID, "参数");
        }
        table.setPhone(validString(root, "phone", 11, YesNoEnum.NO)).setSmsCode(validString(root, "smsCode", 6, YesNoEnum.NO)).setSmsType(validString(root, "smsType", 3, YesNoEnum.NO))
                .setConsumerId(validPrimary(root, "consumerId", YesNoEnum.NO));
        return table;
    }


}
